package com.fantasy.blog.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fantasy.blog.service.ProxyService;
import com.fantasy.blog.vo.Result;
import org.jsoup.Jsoup;
import org.junit.Test;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
public class ProxyServiceImpl implements ProxyService {

    @Override
    public Result getImg() throws IOException {
        String text = Jsoup.connect("http://ovooa.com/API/dmt/api.php")
                .timeout(2000)
                .ignoreContentType(true).execute().body();
        String displayImg = (String) JSON.parseObject(text).get("text");
        Map<String,String> map = new HashMap<>();
        map.put("displayImg",displayImg);
        return Result.success(map);
    }

    @Override
    public Map<String, String> getQQObject(String qq) throws IOException {
        String text = Jsoup.connect("https://api.usuuu.com/qq/"+qq)
                .timeout(2000)
                .ignoreContentType(true).execute().body();
        JSONObject jsonObject = JSON.parseObject(text);
        String nickname = (String) jsonObject.getJSONObject("data").get("name");
        String avatar = (String) jsonObject.getJSONObject("data").get("avatar");

        Map<String,String> map = new HashMap<>();
        map.put("nickname",nickname);
        map.put("avatar",avatar);
        return map;
    }
}
